package pri.syl.xiaowu.websocket.netty.server.init;

import pri.syl.xiaowu.websocket.netty.core.factory.SocketAddressFactory;
import pri.syl.xiaowu.websocket.netty.server.handler.HttpServerInitChannelHandler;
import pri.syl.xiaowu.websocket.netty.server.handler.core.factory.constant.IdleStateConstant;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.Data;


/**
 * ClassName: WebsocketServerInit
 **/
@Data
public class WebsocketServerInit {
    private String url;
    private int websocketMaxFramePayloadLength;
    private int websocketReadTimeoutMilli;

    public WebsocketServerInit(String url, int websocketMaxFramePayloadLength, int websocketReadTimeoutMilli) {
        this.url = url;
        this.websocketMaxFramePayloadLength = websocketMaxFramePayloadLength;
        this.websocketReadTimeoutMilli = websocketReadTimeoutMilli;
    }

    public void init() throws InterruptedException {
        new ServerBootstrap().group(new NioEventLoopGroup(), new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                //绑定通道触发
                .childHandler(new HttpServerInitChannelHandler(IdleStateConstant.HTTP_READER_IDLE_TIME,
                        IdleStateConstant.HTTP_WRITER_IDLE_TIME,
                        IdleStateConstant.HTTP_ALL_IDLE_TIME,
                        IdleStateConstant.HTTP_MAX_CONTENT_LENGTH,
                        websocketMaxFramePayloadLength,
                        websocketReadTimeoutMilli))
                .bind(SocketAddressFactory.getSocketAddress(url))
                .sync()
                .channel()
                .closeFuture()
                .sync();

        
    }
}
